<?php

require_once(ABSPATH . '/wp-admin/admin-functions.php');
require_once(ABSPATH . '/wp-admin/upgrade-schema.php');
// Functions to be called in install and upgrade scripts
function upgrade_all() {
	global $wp_current_db_version, $wp_db_version;
	global $blog, $blogdata, $wpdb, $optiondomain;
//	$wpdb->query("LOCK TABLES $wpdb->blogs WRITE, $wpdb->categories WRITE, $wpdb->comments WRITE, $wpdb->linkcategories WRITE, $wpdb->links WRITE, $wpdb->options WRITE, $wpdb->post2cat WRITE, $wpdb->postmeta WRITE, $wpdb->posts WRITE, $wpdb->usermeta WRITE, $wpdb->users WRITE, $wpdb->postsearch WRITE, $wpdb->sessions WRITE");

	$optiondomain = 'system';
	$wp_current_db_version = __get_option('db_version');

	if (!$wp_current_db_version)
		die('For some reason your DB version is not set. See Upgrade.txt or contact Lyceum support.');

	// We are up-to-date.  Nothing to do.
	if ( $wp_db_version == $wp_current_db_version )
		die('Your database is already up to date');

	// If the version is not set in the DB, try to guess the version.
	// if ( empty($wp_current_db_version) ) {
	// 	$wp_current_db_version = 0;


	if ($wp_current_db_version > $wp_db_version)
		die("db_version is not set to something this update script recognizes. The highest DB version I know how to deal with is $wp_db_version. Your DB version is $wp_current_db_version");

	for($version = $wp_current_db_version; $version < $wp_db_version; $version++)
		eval('upgrade_from_'.$version.'();');

	echo "<h2>Upgrading system options</h2>";
	$optiondomain = 'system';
	populate_system_options();

	$blogs = $wpdb->get_results("SELECT slug, id FROM $wpdb->blogs");
	$optiondomain = 'blog';
	echo "<h2>Upgrading options, roles, and URLs for each blog</h2>";
	foreach( $blogs as $b )	{
		$blogdata = $b;
		$blog = $blogdata->id;
		wp_cache_init();
		echo "<br/>&bull;Upgrading options and roles for blog $blog";
		populate_options();
		populate_roles();

		$wpr = new WP_Rewrite();
		$wpr->flush_rules();
	}
	
	$optiondomain = 'system';
	update_option('db_version', $wp_db_version);
//	$wpdb->query("UNLOCK TABLES");
	}

function upgrade_from_1(){
	global $wpdb, $optiondomain;

	echo "<h2>Upgrading schema from version 1 to version 2</h2>";
	echo "<br/>&rarr; Setting default column values for MySQL 5 compatibility.";
	$wpdb->query("ALTER TABLE $wpdb->categories ALTER category_description SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->comments ALTER comment_author SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->linkcategories ALTER cat_name SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->links ALTER link_notes SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->options ALTER option_value SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->options ALTER option_description SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->postmeta ALTER meta_key SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->postmeta ALTER meta_value SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->posts ALTER post_content SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->posts ALTER post_content SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->usermeta ALTER meta_value SET DEFAULT ''");
	$wpdb->query("ALTER TABLE $wpdb->postsearch MODIFY textdata TEXT NOT NULL");
	$wpdb->query("ALTER TABLE $wpdb->postsearch MODIFY post_id int NOT NULL");
	$wpdb->query("ALTER TABLE $wpdb->postsearch MODIFY object_id int NOT NULL");
	$wpdb->query("ALTER TABLE $wpdb->postsearch MODIFY component enum('title', 'content','comment') NOT NULL");
	$wpdb->query("ALTER TABLE $wpdb->sessions ALTER id SET DEFAULT ''");
	
	echo "<br/>&rarr; Making all blog slugs lowercase<br/>";
	$blogs = $wpdb->get_results("SELECT id, slug FROM $wpdb->blogs");
	foreach( $blogs as $blog ) {
		$newslug = strtolower($blog->slug);
		if($newslug != $blog->slug) {
			echo "&bull;changing $blog->slug to $newslug";
				$wpdb->query("
				UPDATE $wpdb->blogs
				SET slug = '$newslug'
				WHERE id = '$blog->id'
					");
			echo "&bull;deleting $blog->slug's 'home' option (to be refreshed shortly...)";		
			$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'home' AND option_domain = 'blog' AND blog = '$blog->id'");
			}
		}

	echo "<br/>&rarr; Making all usernames lowercase<br/>";
	$users = $wpdb->get_results("SELECT id, user_login FROM $wpdb->users");
 	foreach ($users as $user) {
		$newlogin = strtolower($user->user_login);
		if($newlogin != $user->user_login) {
			echo "&bull;changing $user->user_login to $newlogin";
			$wpdb->query("
				UPDATE $wpdb->users
				SET user_login = '$newlogin'
				WHERE id = '$user->id'
			");
    }
}

	echo "<br/>&rarr; Adding index to force blog slug uniqueness at database level.<br/>";
	$wpdb->query("ALTER TABLE $wpdb->blogs ADD UNIQUE INDEX (slug)");
}

function upgrade_from_2(){
	global $blog, $wpdb;

	echo "<h2>Upgrading schema from version 2 to version 3</h2>";

	echo '<br/>&rarr; Deleting site-wide siteurl option. (Each blog will populated with its own at the end of this upgrade process)';
	$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'siteurl' AND option_domain = 'system'");

	echo '<br/>&rarr; Deleting site-wide blog_charset option. (renamed to system_charset)';
	$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'blog_charset' AND option_domain = 'system'");

	echo '<br/>&rarr; Creating new post2cat index (See <a href="http://lyceum.ibiblio.org/2007/03/02/last-night-a-b-tree-saved-my-life-or-how-to-do-a-simple-index-audit-to-fix-a-slow-query/">this article</a> for more information).';

	$wpdb->query(<<<SQL
		ALTER TABLE $wpdb->post2cat ADD UNIQUE INDEX category_id (category_id,post_id)
SQL
	);
}

// Version of get_option that is private to install/upgrade.
function __get_option($setting) {
	global $wpdb;

	$option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");

	if ( 'home' == $setting && '' == $option )
		return __get_option('siteurl');

	if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
		$option = preg_replace('|/+$|', '', $option);

	@ $kellogs = unserialize($option);
	if ($kellogs !== FALSE)
		return $kellogs;
	else
		return $option;
}

?>
